package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/10/18 21:25
 **/
public class Day46_2_Num231_IsPowerOfTwo {
    class Solution {
        public boolean isPowerOfTwo(int n) {
            long i = 1;
            while(i <= n){
                if(i == n) return true;
                i *= 2;
            }
            return false;
        }
    }
    class Solution2 {
        public boolean isPowerOfTwo(int n) {
            // 2的幂的二进制位
            // 1 -> 1      &  0  = 0
            // 2 -> 10     &  1 = 0
            // 4 -> 100    &  11 = 0
            // 8 -> 1000   &  111 -> 0
            // 10 -> 10000 &  1111 = 0
            return n > 0 && (n & (n-1)) == 0;
        }
    }
}